package com.studyroom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.studyroom.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;
import org.springframework.context.annotation.ComponentScan;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    @Select("SELECT * FROM users WHERE username = #{username}")
    User selectByUsername(String username);
    
    @Select("SELECT * FROM users WHERE email = #{email}")
    User selectByEmail(String email);
    
    @Select("SELECT u.*, " +
            "(SELECT COUNT(*) FROM reservations r WHERE r.user_id = u.id) as reservation_count " +
            "FROM users u " +
            "ORDER BY u.created_at DESC LIMIT #{limit}")
    List<Map<String, Object>> selectLatestUsers(Integer limit);

    @Select("SELECT id, username, email, phone, is_admin as isAdmin, " +
            "created_at as createdAt, updated_at as updatedAt " +
            "FROM users")
    List<User> selectAllUsers();

    @Select("SELECT username FROM users WHERE id = #{userId}")
    String getUsernameById(@Param("userId") Long userId);
} 